package com.example.myshop.controller;


import com.example.myshop.entity.Order;
import com.example.myshop.service.OrderService;
import com.example.myshop.util.JsonResult;
import com.example.myshop.vo.OrderVO;
import com.example.myshop.vo.PayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RequestMapping("order")
@RestController
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("create")
    public JsonResult<Order> create(Long aid, Long[] cids, HttpSession session){
        Long uid=getuidFromSession(session);
        String username=getUsernameFromSession(session);
        Order data= orderService.create(aid,cids,uid,username);
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("create1")
    public JsonResult<Order> create1(Long aid, Long[] cids, HttpSession session){
        Long uid=getuidFromSession(session);
        String username=getUsernameFromSession(session);
        Order data= orderService.create1(aid,cids,uid,username);
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("pay")
    public JsonResult<Order> pay(Long oid,HttpSession session){
        orderService.updateStatus(oid,new Date());
        return new JsonResult<>(OK);
    }


    @RequestMapping("listo")
    public JsonResult<List<OrderVO>> findVOByOid(Long oid){
        List<OrderVO> data= orderService.findVOByOid(oid);
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("listu")
    public JsonResult<List<OrderVO>> findVOByUid(HttpSession session){
        List<OrderVO> data= orderService.findVOByUid(getuidFromSession(session));
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("listbu")
    public JsonResult<List<OrderVO>> findBVOByUid(HttpSession session){
        List<OrderVO> data= orderService.findBVOByUid(getuidFromSession(session));
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("listou")
    public JsonResult<List<OrderVO>> findVOByOUid(Long oid,Long uid){
        List<OrderVO> data= orderService.findVOByOUid(oid,uid);
        return new JsonResult<>(OK,data);
    }
//    @RequestMapping({"","/"})
//    public JsonResult<List<OrderVO>> findVOByOid(Integer oid){
//        List<OrderVO> data= orderService.findVOByOid(oid);
//        return new JsonResult<>(OK,data);
//    }

    @RequestMapping("p_status")
    public JsonResult<OrderVO> findVOByOUPid(Long oid,Long pid,HttpSession session){
        Long uid=getuidFromSession(session);
        OrderVO data= orderService.findVOByOUPid(oid,pid);
        System.out.println(data);
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("status")
    public JsonResult<Order> findOrderByOid(Long oid){
        Order data= orderService.findOrderByOid(oid);
        System.out.println(data);
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("business_search")
    public JsonResult<List<PayVO>> paySearch(Long pid){
        List<PayVO> data= orderService.paySearch(pid);
        System.out.println(data);
        return new JsonResult<List<PayVO>>(OK,data);
    }

    @RequestMapping("business_update")
    public JsonResult<Void> updatePStatus(Long oid,Long pid,Integer pStatus){
        orderService.updatePStatus(oid, pid, 1);
        System.out.println("货物状态号："+1);
        return new JsonResult<>(OK);
    }

    @RequestMapping("business_addPriority")
    public JsonResult<Void> updatePriority(Long oid,Long pid,Integer num){
        orderService.updatePriority(oid, pid, num);
        System.out.println("销量增长："+num);
        return new JsonResult<>(OK);
    }

    @RequestMapping("business_subPriority")
    public JsonResult<Void> updateSubPriority(Long oid,Long pid,Integer num){
        orderService.updateSubPriority(oid, pid, num);
        System.out.println("销量减少："+num);
        return new JsonResult<>(OK);
    }

    @RequestMapping("business_order")
    public JsonResult<List<PayVO>> businessOrder(Long businessId){
        List<PayVO> data=  orderService.businessOrder(businessId);
        System.out.println("商家uid："+businessId);
        System.out.println("商家订单："+data);

        return new JsonResult<List<PayVO>>(OK,data);
    }

    @RequestMapping("receive_product")
    public JsonResult<Void> receivePStatus(Long oid,Long pid,Integer pStatus){
        orderService.updatePStatus(oid, pid, 3);
        System.out.println("货物状态号："+1);
        return new JsonResult<>(OK);
    }

    // 5申请退货 6退货完成 7退货驳回
    @RequestMapping("return_product")
    public JsonResult<Void> returnPStatus(Long oid,Long pid,Integer pStatus){
        orderService.updatePStatus(oid, pid, 5);
        System.out.println("货物状态号："+5);
        return new JsonResult<>(OK);
    }

    @RequestMapping("business_return")
    public JsonResult<Void> businessReturn(Long oid,Long pid,Integer pStatus){
        orderService.updatePStatus(oid, pid, 6);
        System.out.println("货物状态号："+6);
        return new JsonResult<>(OK);
    }

    @RequestMapping("business_refuse")
    public JsonResult<Void> businessRefuse(Long oid,Long pid,Integer pStatus){
        orderService.updatePStatus(oid, pid, 7);
        System.out.println("货物状态号："+7);
        return new JsonResult<>(OK);
    }
}
